﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using YAPL.Language.Core.Exceptions;
using YAPL.Language.Core.Types;

namespace YAPL.Language.Core.Lib.GlobalFunctions {
	public class ParseFloat : YAPLFunction {
		private const string ARG = "arg";
		public ParseFloat() : base(FunctionNames.ParseFloat, GCL.Get(ClassNames.GlobalScope)) {}

		[Param(0, ARG)]
		public override YAPLObject Call(Scope scope) {
			ParseScope(scope);
			var arg = scope[ARG].ToYAPLString();
			YAPLObject result = null;
			try {
				result = new YAPLFloat(double.Parse(arg.StrValue, new NumberFormatInfo()));
				return result;
			}
			catch (Exception) {
				throw new YAPLBadArgumentException("Couldn't parse argument", result);
			}
		}

		public override bool CanPrecompute {
			get {
				return true;
			}
		}
	}
}
